package com.newlink.building.secret.mvp.presenter

import com.newlink.building.common_base.base.BaseApplication
import com.newlink.building.common_base.base.BasePresenter
import com.newlink.building.common_base.ext.ss
import com.newlink.building.common_base.model.bean.DeviceListRequest
import com.newlink.building.secret.mvp.contract.Module_Secret_SecretKeyListContract
import com.newlink.building.secret.mvp.model.Module_Secret_SecretKeyListModel

/**
 * Created by xiaodong on 2022/10/2.
 */
class Module_Secret_SecretKeyListPresenter :
    BasePresenter<Module_Secret_SecretKeyListContract.Model, Module_Secret_SecretKeyListContract.View>(),
    Module_Secret_SecretKeyListContract.Presenter {
    override fun createModel(): Module_Secret_SecretKeyListContract.Model = Module_Secret_SecretKeyListModel()
    override fun getSecretKeyList(
        accessToken: String,
        uId: String,
        requestType: String,
        page: Int,
        limit: Int,
        mac: String
    ) {
        mModel?.getSecretKeyList(accessToken, page, limit)?.ss(mModel, mView) { result ->
            if (result != null) {
                result.data?.list?.let { mView?.refreshSecretList(result.data.total, it) }
            } else {
                mView?.refreshFailed()
            }
        }
    }

    override fun getDeviceList(request: DeviceListRequest) {
        mModel?.getDeviceList(request)?.ss(mModel, mView) { result ->
            if (result != null) {
                result.data?.list.let {
                    if (it != null) {
                        if (BaseApplication.cacheDeviceDatas.size == 0 || BaseApplication.cacheDeviceDatas.size < it.size) {

                            BaseApplication.memoryCacheDeviceData(it)
                        }
                    }
                }
            }
        }
    }
}